import { BasicColumn, FormSchema } from "/@/components/Table"
import { h } from "vue"
import { render } from "/@/utils/common/renderUtils"
import { DescItem } from "/@/components/Description/index"
import PayNode from "./PayNode.vue"
import { formatToDateTime } from "/@/utils/dateUtil"
import { listAllPayWay } from "/@/api/pay"

const getPayOrderStatusColor = (text) => {
  let color = "green"
  if (text == "0") color = "geekblue"
  if (text == "1") color = "purple"
  if (text == "2") color = "green"
  if (text == "3") color = "red"
  if (text == "13") color = "cyan"
  if (text == "5") color = "pink"
  if (text == "6") color = "orange"
  return color
}
const getRefundStatusColor = (text) => {
  let color = "green"
  if (text == "10") color = "purple"
  if (text == "14") color = "gold"
  if (text == "15") color = "green"
  return color
}
export const columns: BasicColumn[] = [
  {
    title: "支付金额",
    width: 80,
    fixed: "left",
    dataIndex: "payAmount",
    customRender: ({ text }) => {
      return text + "元"
    },
  },
  {
    title: "退款金额",
    width: 80,
    dataIndex: "refundAmount",
    customRender: ({ text }) => {
      return text + "元"
    },
  },
  {
    title: "手续费",
    width: 80,
    dataIndex: "mchFeeAmount",
    customRender: ({ text }) => {
      return text + "元"
    },
  },
  {
    title: "订单号",
    width: 210,
    dataIndex: "orderNo",
    slots: { customRender: "orderNo" },
  },
  {
    title: "商户名称",
    width: 130,
    dataIndex: "mchName",
  },

  {
    title: "支付状态",
    width: 100,
    dataIndex: "status",
    customRender: ({ text }) => {
      const color = getPayOrderStatusColor(text)
      return render.renderTag(render.renderDict(text, "pay_order_status"), color)
    },
  },
  {
    title: "来源",
    width: 160,
    dataIndex: "sourceName",
  },

  {
    title: "支付方式",
    width: 160,
    dataIndex: "wayName",
  },

  {
    title: "回调状态",
    width: 100,
    dataIndex: "notifyStatus",
    customRender: ({ text }) => {
      let color = "green"
      if (text == "0") color = "#fcbe97"
      if (text == "1") color = "#badc58"

      return render.renderTag(render.renderDict(text, "pay_notify_status"), color)
    },
  },

  {
    title: "创建时间",
    width: 160,
    // customRender: ({text}) => {
    //   return formatToDateTime(Number(text))
    // },
    dataIndex: "createdAt",
  },
]

export const searchFormSchema: FormSchema[] = [
  {
    field: "unionOrderNo",
    label: "订单号",
    component: "Input",
    componentProps: {
      placeholder: "支付/商户/渠道订单号",
    },
    // colProps: {span: 6},
  },
  {
    label: "订单状态",
    field: "status",
    component: "JDictSelectTag",
    componentProps: {
      dictType: "pay_order_status",
      placeholder: "全部",
      chooseOptionLabel: "全部",
    },
    // colProps: {span: 6},
  },
  {
    label: "回调状态",
    field: "notifyStatus",
    component: "JDictSelectTag",
    componentProps: {
      dictType: "pay_notify_status",
      placeholder: "全部",
      chooseOptionLabel: "全部",
    },
  },
  {
    label: "订单来源",
    field: "source",
    component: "JDictSelectTag",
    componentProps: {
      showSearch: true,
      allowClear: true,
      dictType: "pay_order_source",
      placeholder: "全部",
      chooseOptionLabel: "全部",
    },
  },
  {
    label: "支付方式",
    field: "wayCode",
    component: "ApiSelect",
    componentProps: {
      api: listAllPayWay,
      showSearch: true,
      allowClear: true,
      placeholder: "全部",
      resultField: "data",
      labelField: "wayName",
      valueField: "wayCode",
    },
  },
  {
    label: "商户号",
    field: "mchNo",
    component: "Input",
    // colProps: {span: 6},
  },
  {
    label: "应用ID",
    field: "appId",
    component: "Input",
    // colProps: {span: 6},
  },

  {
    field: "fieldTime",
    label: "创建时间",
    component: "RangePicker",
    componentProps: {
      valueType: "Date",
    },
    colProps: { span: 12 },
  },
]

export const refundFormSchema: FormSchema[] = [
  {
    field: "refundAmount",
    label: "退款金额(元)",
    component: "InputNumber",
    required: true,
    componentProps: {
      placeholder: "请输入退款金额",
    },
  },
  {
    field: "refundReason",
    label: "退款原因",
    component: "Input",
    required: true,
    componentProps: {
      placeholder: "请输入退款原因",
    },
  },
]

export const formDetailSchema: DescItem[] = [
  {
    field: "mchTypeValue",
    label: "所属系统",
    span: 2,
  },
  {
    field: "isvNo",
    label: "服务商号",
    span: 2,
  },
  {
    field: "payOrderNo",
    label: "支付订单号",
    span: 2,
  },
  {
    field: "mchNo",
    label: "商户号",
    span: 2,
  },
  {
    field: "mchOrderNo",
    label: "商户订单号",
    span: 2,
  },
  {
    field: "mchName",
    label: "商户名称",
    span: 2,
  },
  {
    field: "payAmount",
    label: "支付金额",
    span: 2,
    render: (curVal, data) => {
      return curVal + "元"
    },
  },
  {
    field: "mchFeeRate",
    label: "手续费",
    render: (curVal, data) => {
      const color = "#fd9644"
      if (!curVal) {
        curVal = 0
      }
      return render.renderTag(curVal, color)
    },
    span: 2,
  },
  {
    field: "mchFeeRate",
    label: "商家费率",
    render: (curVal, data) => {
      const color = "#ffbe76"
      if (!curVal) {
        curVal = 0
      }
      return render.renderTag(curVal + "%", color)
    },
    span: 2,
  },
  {
    field: "status",
    label: "订单状态",
    render: (curVal, data) => {
      const color = getPayOrderStatusColor(curVal)
      return render.renderTag(render.renderDict(curVal, "pay_order_status"), color)
    },
    span: 2,
  },
  {
    field: "notifyStatus",
    label: "回调状态",
    span: 2,
    render: (curVal, data) => {
      let color = "green"
      if (curVal == "0") color = "#fcbe97"
      if (curVal == "1") color = "#badc58"

      return render.renderTag(render.renderDict(curVal, "pay_notify_status"), color)
    },
  },
  {
    field: "appId",
    label: "应用APPID",
    span: 2,
  },
  {
    field: "errCode",
    label: "支付错误码",
    span: 2,
  },
  {
    field: "errMsg",
    label: "支付错误描述",
    span: 2,
  },
  {
    field: "expiredTime",
    label: "订单失效时间",
    // render: (curVal, data) => {
    //   return formatToDateTime(Number(curVal))
    // },
    span: 2,
  },
  {
    field: "successTime",
    label: "支付成功时间",
    // render: (curVal, data) => {
    //   return formatToDateTime(Number(curVal))
    // },
    span: 2,
  },
  {
    field: "createdAt",
    label: "创建时间",
    // render: (curVal, data) => {
    //   return formatToDateTime(Number(curVal))
    // },
    span: 2,
  },
  {
    field: "updatedAt",
    label: "更新时间",
    // render: (curVal, data) => {
    //   return formatToDateTime(Number(curVal))
    // },
    span: 4,
  },
  {
    field: "divider-1",
    label: "",
    render: (curVal, data) => {
      return render.renderDivider()
    },
    span: 4,
  },

  {
    field: "goodsSubject",
    label: "商品标题",
    span: 2,
  },
  {
    field: "goodsBody",
    label: "商品描述",
    span: 2,
  },

  {
    field: "ifCode",
    label: "接口代码",
    span: 2,
  },

  {
    field: "currency",
    label: "货币代码",
    span: 2,
  },

  {
    field: "wayCode",
    label: "支付方式",
    span: 2,
  },

  {
    field: "clientIp",
    label: "客户端IP",
    span: 2,
  },

  {
    field: "channelUser",
    label: "渠道用户标识",
    span: 2,
  },

  {
    field: "channelOrderNo",
    label: "渠道订单号",
    span: 2,
  },

  {
    field: "notifyUrl",
    label: "异步通知地址",
    span: 2,
  },

  {
    field: "returnUrl",
    label: "页面跳转地址",
    span: 2,
  },

  {
    field: "refundStatus",
    label: "退款状态",
    render: (curVal, data) => {
      const color = getRefundStatusColor(curVal)
      return render.renderTag(render.renderDict(curVal, "pay_order_refund_status"), color)
    },
    span: 2,
  },

  {
    field: "refundTimes",
    label: "退款次数",
    span: 2,
  },

  {
    field: "refundAmount",
    label: "退款总额",
    span: 4,
  },

  {
    field: "extParam",
    label: "商户扩展参数",
    span: 24,
  },
  {
    field: "divider-2",
    label: "",
    render: (curVal, data) => {
      return render.renderDividerWithValue("交易数据")
    },
    span: 4,
  },
  {
    field: "tradeType",
    label: "交易类型",
    span: 4,
  },
  {
    field: "tradeContent",
    label: "交易内容",
    span: 4,
  },

  {
    field: "divider-3",
    label: "",
    render: (curVal, data) => {
      return render.renderDividerWithValue("支付数据节点")
    },
    span: 4,
  },
  {
    field: "payNode",
    label: "",
    render: (curVal, data) => {
      console.log("payOrderNo", data.payOrderNo)
      return h(PayNode, { payOrderNo: data.payOrderNo })
    },
    span: 4,
  },
]
